package com.livepass.android.girlspicks.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class CustomProgressBar extends View {

	public CustomProgressBar(Context context) {
		super(context);
		init();
	}

	public CustomProgressBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public CustomProgressBar(Context context, AttributeSet attrs,
			int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init();

	}

	private void init() {
		mProgressPaint = new Paint();
		mProgressPaint.setColor(Color.parseColor("#f2616f"));
		mBackgroundPaint = new Paint();
		mBackgroundPaint.setColor(Color.parseColor("#808080"));
	}

	private int max = 200;
	private int min = 20;

	private int progress1 = 0;
	private int progress2 = 0;
	private Paint mProgressPaint;
	private Paint mBackgroundPaint;

	public void setProgress(int progress1, int progress2) {
		if (progress1==0 && progress2==0) {
			this.progress1 = 1;
			this.progress2 = 1;
		} else {
			this.progress1 = progress1;
			this.progress2 = progress2;
		}
		invalidate();
	}

	public void setMax(int max) {
		this.max = max;
		invalidate();
	}
	public void setMin(int min) {
		this.min = min;
		invalidate();
	}

	public void setProgressColor(int color1, int color2) {
		mProgressPaint.setColor(color1);
		mBackgroundPaint.setColor(color2);
		invalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		float scale = (progress1 + progress2) <= 20 ? (min * 1.0f / max)
				: ((progress1 + progress2) > max ? 1 : (progress1 + progress2)
						* 1.0f / max);
		int maxWidth = (int) (scale * getWidth());
		// int progress1Width = (int) (maxWidth * progress1 *
		// 1.0f/(progress1+progress2));
		int progress2Width = (int) (maxWidth * progress2 * 1.0f / (progress1 + progress2));

		// draw progress1
		canvas.drawRect(getWidth() - maxWidth, 0, getWidth() - progress2Width,
				getHeight(), mProgressPaint);
		// draw progress1
		canvas.drawRect(getWidth() - progress2Width, 0, getWidth(),
				getHeight(), mBackgroundPaint);

		super.onDraw(canvas);
	}

}
